package com.qf.ssm.controller.promotion;

import com.qf.ssm.entity.Commodity;
import com.qf.ssm.entity.promotion.*;
import com.qf.ssm.service.CommodityService;
import com.qf.ssm.service.PromotionService;
import com.qf.ssm.utils.ResponseCode;
import com.qf.ssm.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/promotion")
public class PromotionController {
    @Resource
    private PromotionService promotionService;
    @Resource
    private CommodityService commodityService;

    /**
     * 促销模块  活动信息
     *
     * @param saName
     * @param saStartTime
     * @param saEndTime
     * @return
     */
    @GetMapping("/reduce")
    private ResponseResult<List<SpecialShow>> selectBysaName(@RequestParam String saName, @RequestParam(required=false) String saStartTime, @RequestParam(required=false) String saEndTime){
        return ResponseResult.success(promotionService.queryBysaName(saName,saStartTime,saEndTime));
    }

    /**
     * 促销模块  添加活动
     *
     * @param specialShow
     *         ss_joint_user_id,
     *         ss_activity_kind,
     *         ss_discounted_setting,
     *         ss_discounted_conditions
     *         sa_name,
     *         sa_start_time,
     *         sa_end_time,
     *         sa_rule_id（specialShow表id）
     *
     * @return  返回影响的行数，若为0表示没插入成功
     */
    @PostMapping("/reduce/add")
    private ResponseResult buildActivity(@RequestBody SSData specialShow){

        return ResponseResult.success(promotionService.addActivitys(specialShow));
    }

    /**
     * 促销模块  修改互动状态
     * @param ssId
     * @param saState
     * @return
     */
    @GetMapping("/reduce/upastate")
    private ResponseResult editSale(@RequestParam int ssId,@RequestParam int saState){
        return ResponseResult.success(promotionService.editActivity(ssId, saState));
    }

    /**
     * 促销模块  修改活动订单数量 需传入修改的内容
     * @param id
     * @return
     */
    @GetMapping("/reduce/upda")
    private ResponseResult updActivity(@RequestParam Integer id,String saName,Integer saOrderNumber,Integer saRealPay){
        if (promotionService.editActivity(id, saName, saOrderNumber, saRealPay) > 0){
            return ResponseResult.success(promotionService.editActivity(id, saName, saOrderNumber, saRealPay));
        }else {
            return ResponseResult.error(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    /**
     * 促销模块  查询的修改活动
     * @param id
     * @return
     */
    @GetMapping("reduce/query")
    private ResponseResult<FormData> viewActivity(@RequestParam Integer id){
        if (promotionService.queryActivity(id) != null){
            return ResponseResult.success(promotionService.queryActivity(id));
        }else {
            return ResponseResult.error(ResponseCode.BRAND_SELECT);
        }
    }


    /**
     * 促销模块  删除或下线活动（修改活动状态：0为失效，3为结束）
     * @param id
     * @return
     */
    @GetMapping("/reduce/remove")
    private ResponseResult removeAcitivity(@RequestParam Integer id,@RequestParam Integer state){
        if (promotionService.updateState(id, state) > 0){
            return ResponseResult.success(promotionService.updateState(id, state));
        }else {
            return ResponseResult.error(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }


    /**
     * 秒杀模块  删除或下线活动（修改活动状态：0为失效，3为结束）
     * @param id
     * @param state
     * @return
     */
    @GetMapping("/seckill/remove")
    private ResponseResult removeSekillAcitivity(@RequestParam Integer id,@RequestParam Integer state){
        if (promotionService.updateSekillState(id, state) > 0){
            return ResponseResult.success(promotionService.updateSekillState(id, state));
        }else {
            return ResponseResult.error(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    /**
     * 秒杀活动  修改活动订单数量，通过id进行修改，需传入修改的值
     * @param id
     * @param saName
     * @param saOrderNumber
     * @param saRealPay
     * @return
     */
    @PostMapping("/sekill/edit")
    private ResponseResult updSekillActivity(@RequestParam Integer id,String saName,Integer saOrderNumber,Integer saRealPay){
        if (promotionService.updateSekillActivity(id, saName, saOrderNumber, saRealPay) > 0){
            return ResponseResult.success(promotionService.updateSekillActivity(id, saName, saOrderNumber, saRealPay));
        }else {
            return ResponseResult.error(ResponseCode.PROMO_EDIT_EXCEP);
        }
    }

    /**
     * 通过id 查询修改之后的活动表中
     * @param id
     * @return
     */
    @GetMapping("/sekill/upquery")
    private ResponseResult selectSekillActivity(@RequestParam Integer id){
        return ResponseResult.success(promotionService.querySekillActivit(id));
    }

    /**
     * 秒杀模块  修改活动状态
     * @param ssId
     * @param saState
     * @return
     */
    @GetMapping("/sekill/upastate")
    private ResponseResult editSekillState(@RequestParam int ssId,@RequestParam int saState){
        return null;
    }

    /**
     * 构建秒杀活动专场信息
     * @param buildSekill
     * @return
     */
    @PostMapping("/kill/build")
    private ResponseResult buildSekills(@RequestBody BuildSekill buildSekill){
        int value = promotionService.insertSekillActivity(buildSekill);
        if (value > 0){
            System.out.println("jinlail ");
            return ResponseResult.success(value);
        }else {
            System.out.println("错误"+"error");
            return ResponseResult.error();
        }
    }

    /**
     * 查询秒杀模块中的信息
     * @param saName
     * @param saStartTime
     * @param saEndTime
     * @return
     */
    @GetMapping("/sekill")
    private ResponseResult sekillActivity(@RequestParam String saName, @RequestParam(required=false) String saStartTime, @RequestParam(required=false) String saEndTime){

        return ResponseResult.success(promotionService.querySekillActivity(saName,saStartTime,saEndTime));
    }

    /**
     *组合套装模块查询功能
     * @param saName
     * @param saStartTime
     * @param saEndTime
     * @return
     */
    @GetMapping("/group")
    private ResponseResult<FormData> groupActivity(@RequestParam String saName, @RequestParam(required=false) String saStartTime, @RequestParam(required=false) String saEndTime){
        return ResponseResult.success(promotionService.queryGroupActivity(saName,saStartTime,saEndTime));
    }

    /**
     * 组合模块中修改活动状态 通过(ss)id,(0为未开始，1为进行）
     * @param id
     * @return
     */
    @GetMapping("/group/state")
    private ResponseResult groupActivity(@RequestParam Integer id,@RequestParam Integer state){
        return ResponseResult.success(promotionService.editGroupState(id, state));
    }

    /**
     * 假删除  将组合某活动活动删除 通过id
     * @param id
     * @return
     */
    @GetMapping("/group/del")
    private ResponseResult delGroupActivity(@RequestParam Integer id){
        int result = promotionService.delOneGroup(id);
        return ResponseResult.success(result);
    }

    /**
     * 修改组合模块活动中的信息
     * @param formData
     * @return
     */
    @PostMapping("/group/edit")
    private ResponseResult editGroupActivity(@RequestBody FormData formData){
        int value = promotionService.updateGroup(formData);
        if (value > 0){
            return ResponseResult.success(value);
        }else {
            return ResponseResult.error();
        }
    }

    /**
     * 组合模块  查询修改之后的活动信息
     * @param ssId
     * @return
     */
    @GetMapping("/group/query")
    private ResponseResult queryGroupActivity(@RequestParam int ssId){
        return ResponseResult.success(promotionService.queryGroup(ssId));
    }

    /**
     * 拼团模块  通过活动名查询活动
     * @param saName
     * @param saStartTime
     * @param saEndTime
     * @return
     */
    @GetMapping("/combination")
    private ResponseResult combinationActivity(@RequestParam String saName, @RequestParam(required=false) String saStartTime, @RequestParam(required=false) String saEndTime){
        return ResponseResult.success(promotionService.queryCombinations(saName, saStartTime, saEndTime));
    }

    /**
     * 拼团模块  修改状态 1为未开始,2为进行
     * @param ssId
     * @param saState
     * @return
     */
    @GetMapping("/combine/updState")
    private ResponseResult editCombineActivity(@RequestParam int ssId,@RequestParam int saState){

        return ResponseResult.success(promotionService.updateCombineActivity(ssId, saState));
    }

    /**
     * 拼团模块   删除功能 通过id
     * @param ssId
     * @return
     */
    @GetMapping("/combine/del")
    private ResponseResult delCombineActivity(@RequestParam int ssId){
        int value = promotionService.delCombineActivity(ssId);
        return ResponseResult.success(value);
    }

    /**
     * 拼团模块  修改活动信息
     * @param formData
     * @return
     */
    @GetMapping("/combine/edit")
    private ResponseResult editCombineActivity(@RequestBody FormData formData){
        int result = promotionService.editCombine(formData);
        if (result > 0){
            return ResponseResult.success(result);
        }else {
            return ResponseResult.error();
        }
    }

    /**
     * 拼团模块  查询修改后的活动信息
     * @param ssId
     * @return
     */
    @GetMapping("/combine/upaquery")
    private ResponseResult queryCombine(@RequestParam int ssId){
        FormData formData = promotionService.queryCombine(ssId);
        return ResponseResult.success(formData);
    }

    /**
     * 定金模块  修改活动状态
     * @param ssId
     * @param saState
     * @return
     */
    @GetMapping("/appoint/edit")
    private ResponseResult editAppointState(@RequestParam int ssId,@RequestParam int saState){
        int result = promotionService.editAppointSta(ssId, saState);
        return ResponseResult.success(result);
    }

    /**
     * 定金模块  通过活动名称，时间等查询活动信息
     * @param saName
     * @param saStartTime
     * @param saEndTime
     * @return
     */
    @GetMapping("/appoint")
    private ResponseResult queryAppoints(@RequestParam String saName, @RequestParam(required=false) String saStartTime, @RequestParam(required=false) String saEndTime){
        SpecialShow specialShow = promotionService.queryAppoints(saName, saStartTime, saEndTime);
        return ResponseResult.success(specialShow);
    }

    /**
     * 定金模块  伪删除，通过id
     * @param ssId
     * @return
     */
    @GetMapping("/appoint/del")
    private ResponseResult delAppoints(@RequestParam int ssId){
        int result = promotionService.delAppoint(ssId);
        return ResponseResult.success(result);
    }

    /**
     * 定金模块  修改活动信息
     * @param formData
     * @return
     */
    @PostMapping("/appoint/edit")
    private ResponseResult editAppointActivity(@RequestBody FormData formData){
        int i = promotionService.editAppoint(formData);
        return ResponseResult.success(i);
    }

    /**
     * 定金模块  查询修改之后的活动信息
     * @param ssId
     * @return
     */
    @GetMapping("/appoint/upaquery")
    private ResponseResult queryAppoint(@RequestParam int ssId){
        FormData formData = promotionService.queryAppointActivity(ssId);
        return ResponseResult.success(formData);
    }

    /**
     * 查询部分商品信息
     * @param goodsName
     * @param goodsType
     * @return
     */
    @GetMapping("/goods/info")
    private ResponseResult queryGoodsInfo(@RequestParam String goodsName,@RequestParam String goodsType){
        List<Commodity> commodities = commodityService.queryGoods(goodsName, goodsType);
        return ResponseResult.success(commodities);
    }





}
